namespace ACEMPortal.UserControls
{
	using System;
	using System.Configuration;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using WorkflowProcesses = ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
    using ScenarioFramework.Modules.DM.DataBase;
    using ScenarioFramework.Modules.DM.DataModel;
    using ScenarioFramework.Modules.DM.Application;


	/// <summary>
	///		Summary description for DocumentInfo.
	/// </summary>
	public partial class DocumentInfo : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.DataGrid dbDocumentStates;
		
		protected DocumentManagerDBHelper dmDBHelper = null;

		private XMLDocumentInstance document = null;

		private string sDownloadFile = string.Empty;
		private string sDocumentVersion=string.Empty;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			document = (XMLDocumentInstance) Session["DocumentInstance"];
			sDocumentVersion=(string)Session["DocumentVersion"];
            if (document == null)
                return;

			if ( Page.IsPostBack )
			{
				sDownloadFile = (string)ViewState["DownloadFile"];
			}
			else
			{
				dmDBHelper = new DocumentManagerDBHelper();

				this.txtTitle.Text = document.Name;

				//retrieve TemplateDocument
				this.txtDescription.Text = document.XMLDocumentFile.Description;

				//
				DocumentHelper docHelper=new DocumentHelper (dmDBHelper);
				string sIdFile = docHelper.GetInstanceFileName(document.Id , sDocumentVersion, document.XMLDocumentFile.Id, document.XMLDocumentFile.Type);
				string sFileName = docHelper.GetInstanceFileNameWithoutPath(document.Id , sDocumentVersion, document.XMLDocumentFile.Id, document.XMLDocumentFile.Type);
				
				try
				{
					System.IO.File.Copy(sIdFile, ConfigurationSettings.AppSettings["DownloadDirectory"] + sFileName , true);
				}
				catch (System.Exception err)
				{
                    System.Diagnostics.Trace.Write(err.Message);
                }

                sDownloadFile = ConfigurationSettings.AppSettings["DMDownloadUrl"] + sFileName;

				ViewState["DownloadFile"] = sDownloadFile;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion

		protected void btnDownload_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			XMLDocumentVersion documentVersion = document.XMLDocumentVersions.Get(sDocumentVersion);
			documentVersion.XMLLogs.AddStatus(Page.User.Identity.Name, System.DateTime.Now.ToString(), "DOWNLOADED");
			dmDBHelper = new DocumentManagerDBHelper();
			dmDBHelper.SaveDocumentInstance (document, DocumentManagerDBHelper.SaveMode.DOCUMENT);

			Session["DocumentInstance"] = document;

			Response.Redirect ( this.sDownloadFile);
		}

		protected void btnView_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			XMLDocumentVersion documentVersion = document.XMLDocumentVersions.Get(sDocumentVersion);
			documentVersion.XMLLogs.AddStatus(Page.User.Identity.Name, System.DateTime.Now.ToString(), "ACCESSED");
			dmDBHelper = new DocumentManagerDBHelper();
			dmDBHelper.SaveDocumentInstance (document, DocumentManagerDBHelper.SaveMode.DOCUMENT);

			Session["DocumentInstance"] = document;

			Response.Redirect ( this.sDownloadFile);
		}
	}
}
